package com.sundhage.air
{
	import flash.events.*;
	import flash.filesystem.*;
	import flash.net.FileFilter;
	
	public class JSFileHandler extends EventDispatcher
	{
		static public var FILE_LOADED:String = "file_loaded";
		static public var FILE_SAVED:String = "file_saved";
		static public var FILE_CANCEL:String = "file_cancel";
		static public var FILE_COPIED:String = "file_copied";
		static public var FILE_COPIED_CANCEL:String = "file_copied_cancel";
		static public var DIRECTORY_CREATED:String = "directory_created";
		
		private var fileToOpen:File;
		private var fileToSave:File;
		private var fileToCopy:File;
		public var newDirectoryPath:String;
		public var newDirectoryName:String;
		private var fileFilterXML:FileFilter = new FileFilter("Test", "*.xml");
		private var fileFilterPIC:FileFilter = new FileFilter("Test", "*.jpg");
		
		private var contentToSave:String = "";
		public var loadedContent:String = "";
		public var lastFileName:String = "";
		private var destPath:String;
		private var destName:String;
		
		private function openFileSelected(e:Event):void {
			var stream:FileStream = new FileStream();
			
			stream.open(File(e.target), FileMode.READ);
			var fileData:String = stream.readUTFBytes(stream.bytesAvailable);
			loadedContent = fileData;
			dispatchEvent(new Event(FILE_LOADED));
		
		}
		public function openFile():void {
			fileToOpen = new File();
			fileToOpen.browseForOpen("Öppna", [fileFilterXML]);
			fileToOpen.addEventListener(Event.SELECT, openFileSelected);
			fileToOpen.addEventListener(Event.CANCEL, fileCancel);
		
		}
		
		private function copyFileSelected(e:Event):void {
			var file:File = File(e.target);
			lastFileName = file.name;
			var newPath:File = new File(destPath+destName);
			file.copyTo(newPath, true);
			dispatchEvent(new Event(FILE_COPIED));
		}
		
		private function copyFileCancel(e:Event):void {
			dispatchEvent(new Event(FILE_COPIED_CANCEL));
		}
		
		public function copyFile(destPath:String, destName:String, type:String = ".jpg"):void {
			this.destPath = destPath;
			this.destName = destName;
			
			fileToCopy = new File();
			if (type == ".jpg")
				fileToCopy.browseForOpen("Välj fil", [fileFilterPIC]);
			else if (type == ".xml")
				fileToCopy.browseForOpen("Välj fil", [fileFilterXML]);
			
			fileToCopy.addEventListener(Event.SELECT, copyFileSelected);
			fileToCopy.addEventListener(Event.CANCEL, copyFileCancel);
			
		}
		
		private function cbCreateDirectory(e:Event):void {
			var f:File = File(e.target);
			f.createDirectory();
			newDirectoryPath = f.nativePath;
			newDirectoryName = f.name;
			dispatchEvent(new Event(DIRECTORY_CREATED));
		}
		public function createDirectory():void {
			var f:File = File.documentsDirectory;
			f.addEventListener(Event.SELECT, cbCreateDirectory);
			f.browseForSave("Nytt test");
		}
		// STATISK
		public static function copyFileSilent(source:String, dest:String):void {
			var file:File = new File(source);
			file.copyTo(new File(dest));
		}
		public static function saveFileSilent(path:String, data:String):void {
			//trace(path);
			var file:File = new File(path);
			var stream:FileStream = new FileStream();
			stream.open(file, FileMode.WRITE);
			stream.writeUTFBytes(data);
			stream.close();

		}
		public static function createFolderSilent(path:String, folder:String):void {
			var file:File = new File(path+folder);
			file.createDirectory();
		}
		
		
		public static function deleteDirectory(path:String):void {
			var file:File = new File(path);
			try {
				file.deleteDirectory(true);
			} catch (e:Error) {}
		}
		
		
		public function deleteFile(path:String, filename:String):Boolean {
			var fileToDelete:File = new File(path+filename);
			try {
				fileToDelete.deleteFile();
			} catch (e:Error) { return false; }
			return true;
		}
		
		private function fileCancel(e:Event):void {
			loadedContent = "";
			dispatchEvent(new Event(FILE_CANCEL));
		}
		
		private function saveFileSelected(e:Event):void {
			var f:File = e.target as File;
			var p:String = f.nativePath;
			p = handleExtension(p);			
			f = new File(p);
			
			var stream:FileStream = new FileStream();
			stream.open(f, FileMode.WRITE);
			stream.writeUTFBytes(contentToSave);
			stream.close();
			dispatchEvent(new Event(FILE_SAVED));
			
		}
		
		public function saveFile(content:String):void {
			contentToSave = content;
			fileToSave = new File();
			fileToSave.browseForSave("Spara");
			fileToSave.addEventListener(Event.SELECT, saveFileSelected);
			fileToSave.addEventListener(Event.CANCEL, fileCancel);
			
		}
		
		private function handleExtension(fname:String):String {
			var ending:String = fname.substr(fname.length-4, 4);
			ending = ending.toLowerCase();
			if (ending == ".xml") return fname;
			else return fname+".xml";
		}
		
		public function JSFileHandler() {
		}

	}
}